﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Globalization;


namespace BasecampSync
{
    static class Config
    {
        private static RegistryKey myRegKey =
            Registry.CurrentUser.CreateSubKey("Software\\BaseCampSync");

        private static string myBasecampUsername;
        private static string myBasecampPassword;
        private static string myBasecampUrl;

        public static string BasecampUsername
        {
            get { return Config.myBasecampUsername; }
            set { Config.myBasecampUsername = value; }
        }

        public static string BasecampPassword 
        {
            get { return Config.myBasecampPassword; }
            set { Config.myBasecampPassword = value; }
        }

        public static string BasecampUrl
        {
            get { return Config.myBasecampUrl; }
            set { Config.myBasecampUrl = value; }
        }

        public static bool Save()
        {
            try
            {
                myRegKey.SetValue("username", myBasecampUsername.ToBase64());
                myRegKey.SetValue("password", myBasecampPassword.ToBase64());
                myRegKey.SetValue("url", myBasecampUrl.ToBase64());
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static bool Load()
        {
            try
            {
                Config.myBasecampUsername =
                    myRegKey.GetValue("username").ToString().FromBase64();

                Config.myBasecampPassword =
                    myRegKey.GetValue("password").ToString().FromBase64();

                Config.myBasecampUrl =
                    myRegKey.GetValue("url").ToString().FromBase64();
            }
            catch
            {
                return false;
            }

            return true;
        }

        static public string ToBase64(this string toEncode)
        {
            byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(toEncode);

            return System.Convert.ToBase64String(toEncodeAsBytes);
        }

        static public string FromBase64(this string encodedData)
        {
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);

            return ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
        }

        static public bool Evaluation()
        {
            try
            {
                // Test, if evaluation is set
                string evaluation =
                    myRegKey.GetValue("evaluation").ToString().FromBase64();

                try
                {
                    DateTime evaluationEnd = DateTime.ParseExact(evaluation, "MM-dd-YYYY", DateTimeFormatInfo.InvariantInfo);
                    if (DateTime.Now > evaluationEnd)
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
            catch
            {
                // Evaluation not set
                string evaluation =
                    DateTime.Now.AddDays(90).ToString("MM-dd-YYYY");

                myRegKey.SetValue("evaluation", evaluation.ToBase64());
            }

            return true;
        }
    }
}
